from django.shortcuts import render, HttpResponse
from app.models import client, table, order, dishes, bill, EmployeeUser,preTable
from django.http import JsonResponse
import uuid
# Create your views here.


def reception(request):
    return render(request, 'reception.html')


def entervip(request):
    if request.POST:
        name = request.POST.get('name')
        sex = request.POST.get('sex')
        age = request.POST.get('age')
        phone = request.POST.get('phone')
        id_card = request.POST.get('id_card')
        client.objects.create(level='会员', name=name, sex=sex, age=age, phone=phone, idCard=id_card)
        return render(request, 'entervip.html', {"msg": "注册成功"})
        pass
    else:
        return render(request, 'entervip.html')
    pass


def gettable(request):
    return render(request, 'gettable.html')
    pass


def get_table(request):
    if request.POST:
        client_id = request.POST.get('client_id')
        try:
            cli = client.objects.get(id=client_id)
            tables = table.objects.all()
            context = {
                'tables': tables,
                'client_id': client_id
            }
            return render(request, 'reserve.html', context=context)
        except:
            return render(request, 'gettable.html', {"msg": "用户不存在"})
        # if client_id:
        #     tables = table.objects.all()
        #     context = {
        #         'tables': tables,
        #         'client_id': client_id
        #     }
        #     return render(request, 'reserve.html', context=context)
        # else:
        #     return render(request, 'gettable.html', {"msg": "用户不存在"})
    else:
        return render(request, 'gettable.html')
    pass


def choicetable(request, tableid, clientid):
    tab = table.objects.get(id=tableid)
    tab.state = False
    tab.save()
    cli = client.objects.get(id=clientid)
    preTable.objects.create(vipid=cli, tableid=tab)
    return HttpResponse("订桌成功")
    pass


def set_order(request):
    return render(request, 'bill.html')


def get_order(request):
    if request.POST:
        client_id = request.POST.get('client_id')

        try:
            cli = client.objects.get(id=client_id)
            order_message = order.objects.filter(clientid=client_id)
            context = {
                'cli': cli,
                'order_message': order_message,
            }
            return render(request, 'getbill.html', context=context)
        except:
            return render(request, 'bill.html', {"msg": "用户不存在"})
        # if cli:
            # order_message = order.objects.filter(clientid=client_id)
            # context = {
            #     'cli': cli,
            #     'order_message': order_message,
            # }
            # return render(request, 'getbill.html', context=context)
        # else:
        #     return render(request, 'bill.html', {"msg": "用户不存在"})
    return render(request, 'bill.html')


def set_bill(request):
    clinum = request.POST.get("cli")
    cliallcost = request.POST.get("cliallcost")
    cli = client.objects.get(id=clinum)
    cli.allCost = cli.allCost+int(cliallcost)
    cli.save()
    ords = order.objects.filter(clientid=clinum)
    billId = str(uuid.uuid1())
    # ordlist = []
    for ord in ords:
        emp = EmployeeUser.objects.get(id=ord.operatorid.id)
        tab = table.objects.get(id=ord.tableid.id)

        if ord.done:
            dis = dishes.objects.get(id=ord.dishesid.id)
            # ordlist.append(dis)
            bill.objects.create(clientId=cli, operatorId=emp, dishId=dis, tableId=tab, billId=billId)
    tab.state = True
    tab.save()
    ords.delete()
    # billId = str(uuid.uuid1())
    # bill.objects.create(billId=billId)
    resule = {}
    resule['resule'] = "结账成功"
    return JsonResponse(resule)
    pass
